/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.put.dcs_modeler.computingObjectEditor.actions;

import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CookieAction;
import org.openide.windows.TopComponent;
import pl.put.dcs_modeler.computingObjectEditor.ComputingObjectEditorTopComponent;
import pl.put.dcs_modeler.fileTypeDataLoader.dataObjects.DCSComputingObjectDataObject;

public final class OpenAction extends CookieAction
{

    @Override
    protected void performAction( Node[] activatedNodes )
    {
        DCSComputingObjectDataObject dataObject = null;
        if ( activatedNodes.length > 0 )
        {
            dataObject = activatedNodes[0].getLookup().lookup( DCSComputingObjectDataObject.class );
        }
        else
        {
            try
            {
                activatedNodes = getActivatedNodes();
                FileObject fileObject = activatedNodes[0].getLookup().lookup( FileObject.class );
                DataObject dObj = DataObject.find( fileObject );
                if ( dObj instanceof DCSComputingObjectDataObject )
                {
                    dataObject = ( DCSComputingObjectDataObject )dObj;
                }
            }
            catch ( DataObjectNotFoundException ex )
            {
                Exceptions.printStackTrace( ex );
            }
        }
        if ( dataObject != null )
        {
            ComputingObjectEditorTopComponent window = ComputingObjectEditorTopComponent.findInstance( dataObject );
            if ( window != null )
            {
                window.open();
                window.requestActive();
            }
        }
    // TODO use dataObject
    }

    @Override
    protected int mode()
    {
        return CookieAction.MODE_EXACTLY_ONE;
    }

    public String getName()
    {
        return NbBundle.getMessage( OpenAction.class, "CTL_OpenAction" );
    }

    @Override
    protected Class[] cookieClasses()
    {
        return new Class[]
                {
                    DataObject.class
                };
    }

    @Override
    protected String iconResource()
    {
        return "pl/put/resources/workflow_simple_case_16x16.png";
    }

    public HelpCtx getHelpCtx()
    {
        return HelpCtx.DEFAULT_HELP;
    }

    @Override
    protected boolean asynchronous()
    {
        return false;
    }
}

